home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Diamond Collection
/
The Diamond Collection (Software Vault)(Digital Impact).ISO
/
cdr44
/
speller2.zip
/
SPELLER.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-13
|
11KB
|
628 lines
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <ctype.h>
#include <conio.h>
#if defined (_MSC_VER) || defined (__WATCOMC__)
#include <graph.h>
#endif
#include "spell.hpp"
const int MAXWORD=40,
MAXLINE =81;
char line[MAXLINE];
char separator[MAXLINE];
char *iptr=NULL; //ptr to word;
int ypos;
int xbegin,xend;
FILE *fp,*fpout;
char str[132];
char *wptr;
int cline;
int cword;
int Abort=0; // abort flag
int Ignore=0;
//#define SPELL EmsSpell
#define SPELL CXmsSpell
//#define SPELL XmsSpell
//#define SPELL DiskSpell
SPELL *sp;
////////////////////////////////////////////////
//
//
///////////////////////////////////////////////////
int isletter(char a){return (isalpha(a) || a=='-');}
#if defined (__SC__)
char static tword[MAXWORD];
#define WHITE 15//_WHITE
#define BLACK 0 //_BLACK
#define LIGHTGRAY 7//_GRAY
char wx1=0,wy1=0,wx=79,wy=24;
char attr=7,fore,back;
char curx,cury;
void clrscr()
{
char wx2=wx1+wx;
char wy2=wy1+wy;
_asm{
/* mov ah,0;
mov al,3;
int 0x10;
mov ah,5;
mov al,0;
int 0x10;*/
mov ah,6 ;
mov al,0 ;
mov bh,0;
mov ch,wy1 ;
mov cl,wx1 ;
mov dh,wy2 ;
mov dl,wx2;
int 0x10 ;
}
}
void textcolor(int color)
{
fore=color &0x0f;
attr=fore+back;
}
void textbackground(int color)
{
if(color == WHITE )
color= LIGHTGRAY;
back=char(color <<4) & 0xf0;
attr=fore+back;
}
void clreol()
{
/*
_asm{
mov ah,7
mov al,0
mov ch,24
mov cl,79
mov dh,0
mov dl,0
int 10h
} */
}
void gotoxy(int x,int y)
{
char c_x=x-1+wx1;
char c_y=y-1+wy1;
curx=x;
cury=y;
asm { mov ah,2;
mov bh,0;
mov dh,c_y;
mov dl,c_x;
int 0x10;
}
}
lf()
{
if(cury > wy)
{
// scroll
char wx2=wx1+wx,wy2=wy1+wy;
_asm {
mov ah,6 ;
mov al,1 ;
mov bh,0;
mov ch,wy1 ;
mov cl,wx1 ;
mov dh,wy2 ;
mov dl,wx2;
int 0x10 ;
}
}
else
cury++;
}
void cputs(char *msg)
{
char chr;
while(*msg ) {
chr=*msg++;
if(chr == '\r'){
gotoxy(1,cury);
continue;
}
if(chr == '\n')
{ lf();
gotoxy(curx,cury);
continue;
}
_asm{
mov ah,0x09;
mov al,chr ;
mov bh,0;
mov bl,attr;
mov cx,1 ;
int 0x10;
}
curx++;
if(curx >wx)
{curx=1;
lf();
}
gotoxy(curx,cury);
}
}
void window(int x1,int y1,int x2,int y2)
{
wx1=x1-1;
wy1=y1-1;
wx=x2-x1;
wy=y2-y1;
gotoxy(1,1);
//clrscr()
}
int wherex()
{
char pos;
asm { mov ah,3
MOV BH,0;
int 0x10;
mov pos,dl
}
return int(pos)+1-wx1;
}
int wherey()
{
char pos;
asm { mov ah,3
MOV BH,0;
int 0x10;
mov pos,dh
}
return int(pos)+1-wy1;
}
#endif
#if defined (_MSC_VER) || defined( __WATCOMC__ )
#define WHITE 15//_WHITE
#define BLACK 0 //_BLACK
#define LIGHTGRAY 7//_GRAY
void clrscr()
{
_clearscreen( _GWINDOW );
}
void textcolor(int color)
{
_settextcolor(color);
}
void textbackground(long color)
{
_setbkcolor(color);
}
void clreol()
{
// _clearscreen( _GCLEARSCREEN );
}
void cputs(char *msg)
{
_outtext(msg);
}
void gotoxy(int x,int y)
{
_settextposition(y,x);
}
void window(int x1,int y1,int x2,int y2)
{
_settextwindow(y1,x1,y2,x2);
}
int wherex()
{
struct rccoord rc;
rc = _gettextposition();
return rc.col;
}
int wherey()
{
return (_gettextposition()).row;
}
#endif
char *getword() /* get the next word in the input stream */
{
register char *wp; /* ptr into word */
// register int len; /* length of word */
static char word[MAXWORD]; /* word to return */
nexline:
/* time to read a new line? */
if (iptr == NULL || *iptr == 0) {
if(cline > 0)
{
// write line
fputs(line,fpout);
putc('\n',fpout);
}
if (fgets(line, MAXLINE - 1, fp) == NULL) {
fclose(fp);
return NULL;
}
// remove newline
line[strlen(line)-1]= '\0';
iptr = line;
cline++;
cputs("\r\n");
ypos=wherey();
cputs(line);
}
/* skip off leading non-alphas */
while (*iptr && !isletter(*iptr))
iptr++;
if (*iptr == 0)
goto nexline;
/* and now set the word up */
xbegin=int(iptr-line);
wp = word;
while (*iptr && (isletter(*iptr) || *iptr == '\''))
*wp++ = *iptr++;
xend=int(iptr-line);
/* dump trailing 's && 'd */
if (*(wp-2) == '\'') {
if (*(wp-1) == 's' || *(wp-1) == 'S' ) // || *(wp-1) == 'd'*(wp-1) == 'D'))
wp -= 2;}
*wp = 0;
if(word[0] == '\0') return NULL;
return(word);
}
void change(char *word)
{
// xbegin
int wlen=strlen(word);
int llen=strlen(line);
int oldlen =xend -xbegin ;
if(wlen > oldlen)
{
// insert blanks
memmove(line+xbegin+wlen+1,line+xend,llen-xend);
memcpy(line+xbegin,word,wlen);
memset(line+xbegin+wlen ,' ',1);
}
else
{
memcpy(line+xbegin,word,wlen);
memset(line+xbegin+wlen ,' ',oldlen-wlen);
}
}
void selectWord(char *word)
{
textcolor(BLACK);
textbackground(WHITE);
if(word){
gotoxy(xbegin+1,ypos);
cputs(word);
}
}
void unselectWord(char *word)
{
textcolor(LIGHTGRAY);
textbackground(BLACK);
if(word){
gotoxy(xbegin+1,ypos);
cputs(word);
}
}
int editline(char *wrd)
{
char ch;
int i=0;
// int wlen= strlen(wrd);
int row=wherey();
int result;
textcolor(BLACK);
textbackground(WHITE);
clreol();
cputs(wrd);
gotoxy(1,row);
while (!Abort)
{
ch=getch();
if(ch == 27 )
{
result= 0;
break;
//s_word[0]= '\0';
}
else if(ch == 13 )
{
change(wrd);
result= 1;
break;
}
else if( ch == 0)
{
ch=getch();
switch(ch)
{
case 77:
if( i < 29 )
i++;
break;
case 75:
if( i > 0)
i--;
break;
case 67 : // Ignore All
Ignore=1;
ungetch(27);
break;
case 68 : // Abort
Abort = 1;
result =0;
break;
case 66 : // Ignore //F8
ungetch(27);
break;
case 65 : // Add //F7
sp->add(wrd,sp->tempDic);
ungetch(27);
break;
case 64 : // change //F6
change(wrd);
ungetch(27);
break;
}
}
else if( isascii(ch))
{
wrd[i] = ch;
#if (__SC__)
putc(ch,stdout);
#else
putch(ch);
#endif
if(i < 29)
i++;
}
gotoxy(1+i,row);
}
textcolor(LIGHTGRAY);
textbackground(BLACK);
return result;
}
int main(int argc,char **argv)
{
int mode;
char ch;
char s_word[30]; // suggested word
char *word;
int savex,savey;
/////////////////////////////////////////////////
if (argc!=2)
{
printf("\n\rThe Speller v1.0 Demo Program\n\r Copyright 1995, (c) The Yucatan Electronic Assembly Company\n\r");
printf("\n\r speller - Checks the spelling of a list of words and ouputs to spelled.ok\n\r");
printf("\n\n Syntax: \n\r speller infile \n\r\n\r infile - The file name of the text to be spell checked");
exit(0);
}
if((fp=fopen(argv[1],"r")) == NULL)
{
printf("Error in opening file %s\n",argv[1]);
exit(8);
}
fpout=fopen("spelled.ok","w");
cout <<"loading dictionary...";
sp =new SPELL;
cout <<"\r Done " <<endl;
if(sp->getLastError())
{
delete sp;
cout <<"\r Error Loading Dictionary";
exit(0);
}
mode=1;
s_word[0]=0;
clrscr();
memset(separator,'─',80);
gotoxy(1,16);
cputs(separator);
gotoxy(6,16);
cputs("┤ Suggestion(s)) ├");
gotoxy(26,16);
cputs("F6-replace F7-Add F8-Ignore F9-Ignore All F10-Abort");
window(1,1,80,15);
cline=cword=0;
sp->SetLevel (COMPLEX_MATCH);// |TYPOS_MATCH );
while((word=getword())!= NULL && !Abort)
{
cword++;
if(sp->check(word) == 0) {
if(mode) {
selectWord(word);
savex=wherex();
savey=wherey();
textcolor(LIGHTGRAY);
textbackground(BLACK);
window(1,17,80,25);
clrscr();
cputs("Searching ...");
#if defined (__SC__)
strcpy(tword,word);
sp->suggest(tword);
#else
sp->suggest(word);
#endif
clrscr();
if(sp->suggestion->count > 0 ){
int wlen=strlen(word)+5;
int ncol= 80 / wlen;
int scount = sp->suggestion->count;
for(int i=0;i < scount; i++)
{
gotoxy(1+(i % ncol)*wlen,wherey());
cputs(sp->suggestion->get(i)->name);
if(i % ncol ==( ncol-1))
cputs("\r\n");
}
int row=0,col=0;
i=0;
gotoxy(1,1);
while (!Abort)
{
ch=getch();
if(ch == 0)
{
ch=getch();
switch(ch)
{
case 77 : // right
if(i < scount-1)
i++;
break;
case 75 : // left
if(i > 0 )
i--;
break;
case 72 : // up
if(i >= ncol)
i-=ncol;
break;
case 80 : // down
if( i+ncol < scount)
i+=ncol;
break;
case 68 : // Abort // F10
Abort = 1;
ungetch(27); // put back esc
break;
case 67 : // Ignore All //F9
sp->ignoreDic->insert(word);
goto endfor; // put back esc
case 66 : // Ignore //F8
goto endfor;
case 65 : // Add //F7
sp->add(word,sp->tempDic);
goto endfor;
case 64 : // change //F6
change(sp->suggestion->get(i)->name);
goto endfor;
default:
break;
}
col= i% ncol;
row = i/ ncol;
gotoxy(1+col*wlen,1+row);
}
else if(ch == 13) // return
{
strcpy(s_word,sp->suggestion->get(i)->name);
// ok
break;
}
else if( ch == 27) //escape
{
strncpy(s_word,word,29);
// cancel
break;
}
}
//if(Abort) break;
}
else
{cputs("No suggestion found");
// sp->add(word,sp->tempDic);
strncpy(s_word,word,29);
}
// edit suggestion
gotoxy(1,9);
if(editline(s_word))
{
//change text
}
else {
if(Ignore)
{
Ignore = 0;
sp->ignoreDic->insert(word);
}
}
// cout <<endl;
endfor:
window(1,1,80,15);
gotoxy(savex,savey);
unselectWord(word);
}
else // no suggestions
sp->tempDic->insert(word);
}
}
clrscr();
if( sp->tempDic->modified && !Abort)
{
sp->tempDic->save("temp.dic");
cerr << "Total misspelled word count = " << sp->tempDic->count << endl;
}
delete sp;
return 0;
}